// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

float4 fvLightPos;
//float fG;
//float fG2;
float fExposure;
#define fG  -0.990f
#define fG2  -0.990f * -0.990f

// Calculates the Mie phase function
float getMiePhase(float fCos, float fCos2, float g, float g2)
{
   return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
}

// Calculates the Rayleigh phase function
float getRayleighPhase(float fCos2)
{
   return 0.75 * (2.0 + 0.5 * fCos2);
   //return 0.75 + 0.75*fCos2;
}

float4 ps_main(float4 c0 : TEXCOORD0,
         float4 c1 : TEXCOORD1,
         float3 fvDirection : TEXCOORD2) : COLOR
{
   float fCos = dot(fvLightPos.xyz, fvDirection) / length(fvDirection);
   float fCos2 = fCos*fCos;
   float4 color = getRayleighPhase(fCos2) * c0 + getMiePhase(fCos, fCos2, fG, fG2) * c1;
   color.a = color.b;
   return 1 - exp(-fExposure * color);
}
